<?php
// Validate what page to show
global $module;
// Set modules value
if (isset($_GET['module'])) {
    $module = $_GET['module'];
} elseif (!isset($module) && isset($_SESSION['module'])) {
    $module = $_SESSION['module'];
} else {
    $module = null;
}
// Save value to session
if (isset($module)) {
    $_SESSION['module'] = $module;
}
/**
 * zeus_menu()
 *
 * @return $aMenuItem
 */
function zeus_menu()
{
    // Get current user's privileges from session
    $privileges = get_privilege($_SESSION['user_id']);
    $aPrivileges = explode(',', $privileges['privilege']);
    // Define the menu item by privileges
    $aMenuItem = array();
    $rsCount = count($aPrivileges);
    for($rsIndex = 0;$rsIndex < $rsCount;$rsIndex++) {
        $sql = "SELECT * FROM "
         . DB_PREFIX . "modules WHERE page_id ='" . $aPrivileges[$rsIndex] . "'";
        $aResult = zeus_sql_query($sql);
        // Hidden menu item
        $bShowInMenu = $aResult['show_in_menu'];
        if (1 == $bShowInMenu) {
            $aMenuItem[$rsIndex] = $aResult;
        } else {
            continue;
        }
    }
    // Delete null members
    sort($aMenuItem);
    return $aMenuItem;
}
/**
 * zeus_get_menu()
 *
 * @return
 */
/* DTree menu
function zeus_get_menu()
{
    global $adminMenu;
	$menu = zeus_menu();
    $adminMenu = "<P><A href=\"javascript: AdminMenu.openAll();\">";
    $adminMenu .= _e('Open all');
    $adminMenu .= "</A> | <A href=\"javascript: AdminMenu.closeAll();\">";
    $adminMenu .= _e('Close all');
    $adminMenu .= "</A></P>";
    $adminMenu .= "<SCRIPT type=\"text/javascript\">";
    $adminMenu .= "AdminMenu = new dTree('AdminMenu');";
    $rsCount = count($menu);
    // Draw nodes
    for($rsIndex = 0;$rsIndex < $rsCount;$rsIndex++) {
        // Set node param
        $id = $menu[$rsIndex]['page_id'];
        $pid = $menu[$rsIndex]['parent_id'];
        $name = _e($menu[$rsIndex]['page_name']);
        $url = "admin.php?p=" . $menu[$rsIndex]['page_url'];
        $open = _e($menu[$rsIndex]['open']);
        $adminMenu .= "AdminMenu.add("
         . $id . ","
         . $pid . ",'"
         . $name . "','"
         . $url . "','"
         . $title . "','"
         . $target . "','"
         . $icon . "','"
         . $iconOpen . "',"
         . $open
         . ");";
    }
    // Draw menu
    $adminMenu .= "document.write(AdminMenu);";
    $adminMenu .= "</SCRIPT>";
    return $adminMenu;
}
*/
/**
 * zeus_get_menu()
 *
 * @return
 */
function zeus_get_menu()
{
    foreach(zeus_menu() AS $key => $value) {
        switch ($value['type']) {
            case 'dash':
                $dashmenu[] = $value;
                break;
            case 'admin': $adminmenu[] = $value;
                break;
            case 'side': $sidemenu[] = $value;
                break;
            case 'sub':
                $submenu[$value['parent_id']][] = $value;
                break;
            default:break;
        }
    }
    $menu['dash'] = $dashmenu;
    $menu['admin'] = $adminmenu;
    $menu['side'] = $sidemenu;
    $menu['sub'] = $submenu;
    return $menu;
}
/**
 * zeus_show_menu()
 *
 * @param string $type
 * @return
 */
function zeus_show_menu($type = '')
{
    global $menu;
    switch ($type) {
        case 'dash':
            foreach($menu['dash'] AS $value) {
                $menuToShow .= "\n\t<li><a href='/dashboard/index.php'>" . _e($value['page_name']) . "</a></li>";
            } ;
            break;
        case 'admin':
            foreach($menu['admin'] AS $value) {
                if ($value['page_url'] == $_GET['module']) {
                    $class = "class='current'";
                } else {
                    $class = "";
                }
                $menuToShow .= "\n\t<li><a href='" . $_SERVER['PHP_SELF'] . "?module=" . $value['page_url'] . "' " . $class . ">" .  _e($value['page_name'])  . "</a></li>";
            } ;
            break;
        case 'side':
            foreach($menu['side'] AS $value) {
                if ($value['page_url'] == $_GET['module']) {
                    $class = "class='current'";
                } else {
                    $class = "";
                }
                $menuToShow .= "\n\t<li><a href='" . $_SERVER['PHP_SELF'] . "?module=" . $value['page_url'] . "' " . $class . ">" . _e($value['page_name']) . "</a></li>";
            } ;
            break;
        case 'sub':
            $sql = "SELECT * FROM " . DB_PREFIX . "modules WHERE page_url='" . $_GET['module'] . "';";
            $result = zeus_sql_query($sql);
            $parent_id = $result['page_id'];
            if (!empty($menu['sub'][$parent_id])) {
                foreach($menu['sub'][$parent_id] AS $value) {
                    $menuToShow .= "\n\t<li><a href='/dashboard/" . $value['page_url'] . ".php' " . $class . ">" . _e($value['page_name']) . "</a></li>";
                }
            } else {
                zeus_do_nothing();
            } ;
            break;
        default:break;
    }
    return $menuToShow;
}

?>
<?php
global $menu;
$menu = zeus_get_menu();

?>
<ul id="dashmenu">
<?php
// Show dash menu
echo zeus_show_menu('dash');

?>

</ul>
<ul id="adminmenu">
<?php
// Show admin menu
echo zeus_show_menu('admin');

?>
</ul>
<ul id="sidemenu">
<?php
// Show side menu
echo zeus_show_menu('side');

?>
</ul>
<ul id="submenu">
<?php
// Show sub menu
echo zeus_show_menu('sub');

?>
</ul>
